package com.sl.mes.app.controller.mm;


import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.sl.mes.app.dao.mm.AggMmPickmVo;
import com.sl.mes.app.dao.mm.AggMoVo;
import com.sl.mes.app.service.mm.MmPickmBService;
import com.sl.mes.app.util.JsonHandler;
import com.sl.mes.core.util.JacksonUtil;
import com.sl.mes.core.util.ResponseUtil;
import com.sl.mes.db.vo.MmMo;
import com.sl.mes.db.vo.MmPickmB;
import com.sl.mes.db.vo.MmPmo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mes/mmpickmb")
@Validated
public class MmPickmBController {


    @Resource
    private MmPickmBService mmPickmBService;

    /**
     * 添加备料计划明细
     * @param
     * @param
     * @return
     */
    @PostMapping("insertMmPickmB")
    public Object insertMmPickmB(@RequestBody String body, HttpServletRequest request) {
        JsonNode movo = (JsonNode) JacksonUtil.toNode(body);
        JsonNode mmpickmBody = (JsonNode) ((JsonNode) ((JsonNode) movo.get("data")).get("mmreqpickm")).get("mmReqpickmBBodyVo");
        List<MmPickmB> lstMmPickmB = new ArrayList<>();
        for (int i = 0; i < mmpickmBody.size(); i++) {
            MmPickmB itemVo = JsonHandler.fromJson(mmpickmBody.get(i).toString(), MmPickmB.class);
            lstMmPickmB.add(itemVo);
        }

        for (MmPickmB mmPickmB :lstMmPickmB) {
            mmPickmBService.insertMmPickmB(mmPickmB);
        }
        Map<Object, Object> result = new HashMap<Object, Object>();
        result.put("lstMmPickmB",lstMmPickmB);
        return ResponseUtil.ok(result);
    }
}
